/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.welchapps.nporganizer;


import java.util.ArrayList;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.welchapps.nporganizer.NotePad.DataCount;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;



public class Login extends Activity {

    private EditText mUser, mPass;
    private Button signupbutton, loginbutton;
    ArrayAdapter<CharSequence> adapter_cat, adapter_stat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it in res/layout/note_editor.xml
        setContentView(R.layout.login);
        //final Intent intent = getIntent();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        mUser = (EditText) this.findViewById(R.id.user);
        signupbutton = (Button) this.findViewById(R.id.SignupButton);
        loginbutton = (Button) this.findViewById(R.id.LoginButton);  
        mPass = (EditText) findViewById(R.id.pass);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // If we didn't have any trouble retrieving the data, it is now
        // time to get at the stuff.
        
        if (ParseUser.getCurrentUser() != null){
        	signupbutton.setEnabled(false);
        	loginbutton.setText("Logout");
        } else {
        	signupbutton.setEnabled(true);
        	loginbutton.setText("Login");
        }

    }



    @Override
    protected void onPause() {
        super.onPause();

    }
    
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate menu from XML resource
//        MenuInflater inflater = getMenuInflater();
//        inflater.inflate(R.menu.filter_options_menu, menu);
//
//        return super.onCreateOptionsMenu(menu);
//    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        	finish();
        	return true;  
        default:
            return super.onOptionsItemSelected(item);
        }
    }


    
    public void Login(View view){
    	if (ParseUser.getCurrentUser() != null){
    		ParseUser.logOut();
    		Toast.makeText(getBaseContext(), "You have logged out of the service", Toast.LENGTH_LONG).show();
    		signupbutton.setEnabled(true);
        	loginbutton.setText("Login");
    	} else {
	    	if (mUser.getText().toString().equals("") || mPass.getText().toString().equals("")){
	    		Toast.makeText(getBaseContext(), "Username or password missing.", Toast.LENGTH_LONG).show();
	    		return;
	    	}
	    	ParseUser.logInInBackground(mUser.getText().toString(), mPass.getText().toString(), new LogInCallback() {
	    		  public void done(ParseUser user, ParseException e) {
	    		    if (user != null) {
	    		    	Toast.makeText(getBaseContext(), "Your login is successful", Toast.LENGTH_LONG).show();
	    		    	signupbutton.setEnabled(false);
	    	        	loginbutton.setText("Logout");
	    		    } else {
	    		      switch (e.getCode()) {
	  	    			case ParseException.CONNECTION_FAILED:
	  	    				Toast.makeText(getBaseContext(), "Unable to connect. Check internet connection.", Toast.LENGTH_LONG).show();
	  	    				return;
	  	    			case ParseException.OBJECT_NOT_FOUND:
	  	    				Toast.makeText(getBaseContext(), "Unable to login. Please check username and password and try again.", Toast.LENGTH_LONG).show();
	  	    				return;
	  	    			default:
	  	    				Toast.makeText(getBaseContext(), "An unknown error has occured. Please try again.", Toast.LENGTH_LONG).show();  
	    		      }
	    		      signupbutton.setEnabled(true);
	    		      loginbutton.setText("Login");
	    		    }
	    		  }
	    		});
    	}
    }
    
    public void SignUp(View view){

    	ParseUser user = new ParseUser();
    	if (mUser.getText().toString().equals("") || mPass.getText().toString().equals("")){
    		Toast.makeText(getBaseContext(), "Username or password missing.", Toast.LENGTH_LONG).show();
    		return;
    	}
    	user.setUsername(mUser.getText().toString());
    	user.setPassword(mPass.getText().toString());
    	 
    	user.signUpInBackground(new SignUpCallback() {
    	  public void done(ParseException e) {
    	    if (e == null) {
    	    	Toast.makeText(getBaseContext(), "Your signup is successful", Toast.LENGTH_LONG).show();
    	      	signupbutton.setEnabled(false);
          		loginbutton.setText("Logout");
    	    } else 
    	    	switch (e.getCode()){
    	    	case ParseException.CONNECTION_FAILED:
    	    		Toast.makeText(getBaseContext(), "Unable to connect. Check internet connection.", Toast.LENGTH_LONG).show();
    	    		return;
    	    	case ParseException.USERNAME_MISSING: 
    	    		Toast.makeText(getBaseContext(), "Username or password missing.", Toast.LENGTH_LONG).show();
    	    		return;
    	    	case ParseException.PASSWORD_MISSING:
    	    		Toast.makeText(getBaseContext(), "Username or password missing.", Toast.LENGTH_LONG).show();
    	    		return;
    	    	case ParseException.USERNAME_TAKEN:
    	    		Toast.makeText(getBaseContext(), "Username has already been taken. Please try again.", Toast.LENGTH_LONG).show();
    	    	default:
    	    		Toast.makeText(getBaseContext(), "An unknown error has occured. Please try again.", Toast.LENGTH_LONG).show();
    	    }
    	    signupbutton.setEnabled(true);
        	loginbutton.setText("Login");
    	  }
    	});
    }
    

 

}
